package com.catmiao.ai.config;

import com.catmiao.ai.service.FunctionAssistant;
import com.fasterxml.jackson.databind.util.JSONPObject;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.request.json.JsonObjectSchema;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolExecutor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class LLMConfig {


    @Bean
    public ChatModel chatModel(){
        String apiKey = System.getenv("BAILIAN_API_KEY");

        return OpenAiChatModel.builder()
                .apiKey(apiKey)
                .modelName("qwen-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logResponses(true)
                .logRequests(true)
                .maxRetries(3) // 重试次数
                .build();
    }


    /**
     * 低阶api
     * @param chatModel
     * @return
     */
//    @Bean
//    public FunctionAssistant functionAssistant(ChatModel chatModel){
//
//        // 工具说明
//        ToolSpecification toolSpecification = ToolSpecification.builder()
//                .name("开具发票助手")
//                .description("根据用户提交的开票信息，开具发票")
//                .parameters(
//                        JsonObjectSchema.builder()
//                                .addStringProperty("companyName", "公司名称")
//                                .addBooleanProperty("dutyNumber", "税号序列")
//                                .addStringProperty("amount", "开票金额，保留两位有效数字")
//                                .build()
//                )
//                .build();
//
//        // 工具执行器 业务逻辑
//        ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> {
//            System.out.println("id:" + toolExecutionRequest.id());
//            System.out.println("name:" + toolExecutionRequest.name());
//            String arguments = toolExecutionRequest.arguments();
//            System.out.println("arguments:" + arguments);
//            return "ok";
//        };
//
//        return AiServices.builder(FunctionAssistant.class)
//                .chatModel(chatModel)
//                .tools(Map.of(toolSpecification,toolExecutor))
//                .build();
//    }

    @Bean
    public FunctionAssistant functionAssistant(ChatModel chatModel){
        return AiServices.builder(FunctionAssistant.class)
                .chatModel(chatModel)
                .tools(new InvoiceHandler())
                .build();
    }


 }
